package com.cnhis.cloudhealth.clinical.hosnurse.hosfitadvice.bo;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hosfitadvice.dao.HosFitAdviceDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONObject;
@SuppressWarnings({"rawtypes","unchecked"})
@Service
public class HosFitAdviceBo {
  @Resource
  private HosFitAdviceDao hosFitAdviceDao;
  @Resource
  private GetNewId getNewId;
  private boolean inPatient(Map map)throws Exception{
		return hosFitAdviceDao.inPatient(map)==0;
	}
	private boolean checkAdviceSend(Map map)throws Exception{
		return hosFitAdviceDao.checkAdviceSend(map)>0;
	}
	public ModelVo execNurseVAJUpdate(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		if(inPatient(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("病人不是在院状态，不能调整费用信息");
			return vo;
		}
		if("2".equals(map.get("lSign").toString())&&checkAdviceSend(map)){
			vo.setResult(StaticKeys.OH_FALSE);
			vo.setResultMsg("医嘱已发送停止，不能在调整");
			return vo;
		}
		if("2".equals(map.get("lSign").toString())){
			//修改计价特性 新增修改记录
			map.put("lZAE01", getNewId.nextId());
			hosFitAdviceDao.updateVaf2(map);
			
		}
		return vo;
	}
	
	public ModelVo  execNurseBatchUpdateOrderItem(Map map)throws Exception{
		ModelVo vo = new ModelVo(StaticKeys.OH_SUCCESS);
		String json = map.get("lXML").toString();
		JSONObject jsonObject = JSONObject.fromObject(json);
		List tmpVAF2 = JsonUtil.getList(jsonObject.getJSONArray("VAF2").toString());
		map.put("tmpVAF2", tmpVAF2);
		map.put("lID", getNewId);
		hosFitAdviceDao.updateVbg2(map);
		
		return vo;
	}
	public ModelVo batchAdvice(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosFitAdviceDao.batchAdvice(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	public ModelVo fetchFeeRecord(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=hosFitAdviceDao.fetchFeeRecord(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
	}
	private Map getParams(Map map)throws Exception{
		return hosFitAdviceDao.getParams(map);
	}
	private boolean checkDate()throws Exception{
		return hosFitAdviceDao.checkDate()>0;
	}
	private Map getParameters(Map map)throws Exception{
		 int lVAF01A=0,lVBG01=0,lVAF10=0,lVAF11=0; 
		 
			Map m = getParams(map);
			if(m!=null){
				if(null!=m.get("lvaf01a")){
					lVAF01A = Integer.parseInt(m.get("lvaf01a").toString());
				}
				if(null!=m.get("lvaf10")){
					lVAF10 = Integer.parseInt(m.get("lvaf10").toString());
				}
				if(null!=m.get("lvaf11")){
					lVAF11 = Integer.parseInt(m.get("lvaf11").toString());
				}
			}
			map.put("lVAF01A", lVAF01A);
			map.put("lVBG01", lVBG01);
			map.put("lVAF10", lVAF10);
			map.put("lVAF11", lVAF11);
		return map;
	}
	//*******************************************************************0
	private ModelVo fitCurrentOffice(Map map,ModelVo mo)throws Exception{
		if("0".equals(map.get("lBCK01").toString())){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResult("请先选择执行科室");
			return mo;
		}
		 int lVAF01A=0,lVBG01=0,lVAF10=0,lVAF11=0; 
		 
		Map m = getParams(map);
		if(m!=null){
			if(null!=m.get("lvaf01a")){
				lVAF01A = Integer.parseInt(m.get("lvaf01a").toString());
			}
			if(null!=m.get("lvaf10")){
				lVAF10 = Integer.parseInt(m.get("lvaf10").toString());
			}
			if(null!=m.get("lvaf11")){
				lVAF11 = Integer.parseInt(m.get("lvaf11").toString());
			}
		}
		map.put("lVAF01A", lVAF01A);
		map.put("lVBG01", lVBG01);
		map.put("lVAF10", lVAF10);
		map.put("lVAF11", lVAF11);
		if((lVAF11 == 2 && lVAF10 > 3)||(lVAF11 == 1 &&((lVAF10 > 3) ||(lVAF10 == 3 &&checkDate()) ) )){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResult("只能对未执行医嘱的执行科室进行调整");
			return mo;
			
		}
		//--修改计价信息中执行科室、新增医嘱状态记录
		updateVBD2InsertVBG2(map);
		return mo;
	}
	private void updateVBD2InsertVBG2(Map map)throws Exception{
		map.put("lVBG01", getNewId.nextId());
		hosFitAdviceDao.updateVBD2InsertVBG2(map);
	}
	//********************************************************1
	private void updateAppendOffice(Map map)throws Exception{
		map.put("lVBG01", getNewId.nextId());
		hosFitAdviceDao.updateAppendOffice(map);
	}
	private ModelVo fitAppendOffice(Map map,ModelVo mo)throws Exception{
		if("0".equals(map.get("lBCK01").toString())){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResult("请先选择执行科室");
			return mo;
		}
		 int lVAF01A=0,lVBG01=0,lVAF10=0,lVAF11=0; 
		 
		Map m = getParams(map);
		if(m!=null){
			if(null!=m.get("lvaf01a")){
				lVAF01A = Integer.parseInt(m.get("lvaf01a").toString());
			}
			if(null!=m.get("lvaf10")){
				lVAF10 = Integer.parseInt(m.get("lvaf10").toString());
			}
			if(null!=m.get("lvaf11")){
				lVAF11 = Integer.parseInt(m.get("lvaf11").toString());
			}
		}
		map.put("lVAF01A", lVAF01A);
		map.put("lVBG01", lVBG01);
		map.put("lVAF10", lVAF10);
		map.put("lVAF11", lVAF11);
		if((lVAF11 == 2 && lVAF10 > 3)||(lVAF11 == 1 &&((lVAF10 > 3) ||(lVAF10 == 3 &&checkDate()) ) )){
			mo.setResult(StaticKeys.OH_FALSE);
			mo.setResult("只能对未执行医嘱的执行科室进行调整");
			return mo;
			
		}
		updateAppendOffice(map);
		return mo;
	}
	//******************************************************************************************2
	 private boolean checkStates(Map map)throws Exception{
		 return hosFitAdviceDao.checkStates(map)>0;
	 }
	 private boolean checkStopTime(Map map)throws Exception{
		 return hosFitAdviceDao.checkStopTime(map)>0;
	 }
	 private ModelVo fitStopTime(Map map,ModelVo mo)throws Exception{
		   map = getParameters(map);
			if(checkStates(map)){
				mo.setResult(StaticKeys.OH_FALSE);
				mo.setResult("医嘱还没停嘱，不能调整停嘱时间");
				return mo;
			}
			if(checkStopTime(map)){
				mo.setResult(StaticKeys.OH_FALSE);
				mo.setResult("停嘱时间不能小于医嘱开始时间");
				return mo;
			}
			updateVAF2VBG2(map);
		 return mo;
	 }
	 private void updateVAF2VBG2(Map map)throws Exception{
		 map.put("lVBG01", getNewId.nextId());
		 hosFitAdviceDao.updateVAF2VBG2(map);
	 }
	 //*************************************************************************************2
	//**************************************************************************************3
	 private boolean traceType(Map map)throws Exception{
		 return hosFitAdviceDao.traceType(map)>0;
	 }
     private boolean printTrace(Map map)throws Exception{
		 return hosFitAdviceDao.printTrace(map)>0;
	 }
	 private ModelVo fitTraceAdvice(Map map,ModelVo mo)throws Exception{
		 long lBCK01 = Long.parseLong(map.get("lBCK01").toString());
		 if((lBCK01 < -1) || (lBCK01 > 9)){
			 mo.setResult(StaticKeys.OH_FALSE);
				mo.setResult("请先选择执行单类型");
				return mo;
		 }
		 map = getParameters(map);
		 if(traceType(map)){
			 mo.setResult(StaticKeys.OH_FALSE);
				mo.setResult("只能调整描述性医嘱的执行单类型");
				return mo;
		 }
		 if(printTrace(map)){
			 mo.setResult(StaticKeys.OH_FALSE);
				mo.setResult("此描嘱已打印执行单，不能在进行调整");
				return mo;
		 }
		 updateTraceAdvice(map);
		 return mo;
	 }
	 private void updateTraceAdvice(Map map)throws Exception{
		 map.put("lVBG01", getNewId.nextId());
		 hosFitAdviceDao.updateTraceAdvice(map);
	 }
	 //********************************************************************************************3
	 //*********************************************************************************************4
	 private boolean checkValidateTime(Map map)throws Exception{
		 return hosFitAdviceDao.checkValidateTime(map)==0;
	 }
	 private boolean checkBeginTime(Map map)throws Exception{
		 return hosFitAdviceDao.checkBeginTime(map)>0;
	 }
	 private void updateDateTime(Map map)throws Exception{
		 map.put("lVBG01", getNewId.nextId());
		 hosFitAdviceDao.updateDateTime(map);
	 }
	  private ModelVo fitDateTime(Map map,ModelVo mo)throws Exception{
		  map = getParameters(map);
		  if(checkValidateTime(map)){
				 mo.setResult(StaticKeys.OH_FALSE);
					mo.setResult("医嘱还没校对，不能调整校对时间");
					return mo;
			 }
			 if(checkBeginTime(map)){
				 mo.setResult(StaticKeys.OH_FALSE);
					mo.setResult("校对时间不能小于医嘱开始时间");
					return mo;
			 }
			 updateDateTime(map);
		  return mo;
	  }
	 //***********************************************************************************************4
	  
	  //**************************************6
	  private void updateZeroLable(Map map)throws Exception{
		  map.put("lVBG01", getNewId.nextId());
		  hosFitAdviceDao.updateZeroLable(map);
	  }
	    private ModelVo fitZeroLable(Map map,ModelVo mo)throws Exception{
	    	map = getParameters(map);
	    	updateZeroLable(map);
	    	return mo;
	    }
	  //**************************************
	    private boolean checkTempAdviceStatus(Map map)throws Exception{
	    	return hosFitAdviceDao.checkTempAdviceStatus(map)>0;
	    }
	    private boolean checkTerminationTime(Map map)throws Exception{
	    	return hosFitAdviceDao.checkTerminationTime(map)>0;
	    }
	    private void  updateTerminationTime(Map map)throws Exception{
	    	map.put("lVBG01", getNewId.nextId());
	    	hosFitAdviceDao.updateTerminationTime(map);
	    }
	    private ModelVo terminationTime(Map map,ModelVo mo)throws Exception{
	    	map =getParameters(map);
	    	 if(checkTempAdviceStatus(map)){
				 mo.setResult(StaticKeys.OH_FALSE);
					mo.setResult("医嘱是临嘱或已经停止,不需要调整终止时间");
					return mo;
			 }
			 if(checkTerminationTime(map)){
				 mo.setResult(StaticKeys.OH_FALSE);
					mo.setResult("长嘱终止时间不能小于医嘱上次执行时间");
					return mo;
			 }
			 
			 updateTerminationTime(map);
	    	return mo;
	    }
	public ModelVo execNurseAdviceItemsUpdate(Map map)throws Exception{
		ModelVo mo = new ModelVo();
		String lType = map.get("lType").toString();
		if("0".equals(lType)){//调整执行科室
			mo=fitCurrentOffice(map, mo);
		}
       if("1".equals(lType)){//调整附加执行科室
			mo=fitAppendOffice(map, mo);
		}
       if("2".equals(lType)){//调整停嘱时间
			mo=fitStopTime(map, mo);
		}
       if("3".equals(lType)){//调整描嘱
			mo = fitTraceAdvice(map, mo);
		}
       if("4".equals(lType)){//调整校对时间
			mo = fitDateTime(map, mo);
		}
       if("6".equals(lType)){//调整拆零标识
			mo=fitZeroLable(map, mo);
		}
       if("7".equals(lType)){//调整长嘱终止时间
			mo = terminationTime(map, mo);
		}
      
		mo.setResult(StaticKeys.OH_SUCCESS);
		return mo;
		
		
	}
}
